var config = new Object();
var current_friend_index = null;
var num_friends = 0;
var friends = null;
var access_token = null;

config.appId = "";
config.channelUrl = '';

window.fbAsyncInit = function() { 
	FB.init({
	    appId  : config.appId,
	    status : true, // check login status
	    cookie : true, // enable cookies to allow the server to access the session
	    xfbml  : true, // parse XFBML
	    channelUrl : config.channelUrl , // channel.html file
	    oauth  : true // enable OAuth 2.0
	  });
	
	FB.getLoginStatus(function(response) {
		  if (response.authResponse) {
			  afterLogin();
			  access_token = response.authResponse.accessToken;
			  user_id = response.authResponse.userID;
			  var query = FB.Data.query('select friends_hometown,friends_location from permissions where uid={0}', user_id);
			  	query.wait(function(rows) {
			  			if(rows[0].friends_hometown == 0 ||  rows[0].friends_location == 0) {
			  				$('#after-login').hide();
			  				$('#login-button').show();
			  			}
			  	});
		  } else {
			  $('#login-button').show();
		  }
		});
};

function afterLogin() {
	$('#after-login').show();
	FB.api('/me/friends', function(response) {
		  friends = response.data;
		  num_friends = Object.keys(friends).length;
		  current_friend_index = 1;
		  get_friend_data();
	  });
}

function loginUser() {
  	FB.login(function(response) {
		   if (response.authResponse) {
			$('#login-button').hide();
			afterLogin();
		   }
		}, {scope: 'friends_hometown,friends_location'});
	}

function get_friend_data() {
	s = $('#friends-widget .friends-widget-info');
	var cnt = 0;
	for(userid in friends) {
		FB.api('/'+friends[userid].id, 
			{ fields: ['id', 'name','location','hometown','picture'] , type: 'normal' }, 
			function(response) {
				if(cnt !=0) {
					$(s).clone().appendTo('#friends-widget');
				}
				a = $('#friends-widget .friends-widget-info').filter(':last');
				var lft = cnt*400;
				$(a).css("left",lft);
				$(a).find('.friend-name').html(response.name);				
				if(typeof response.hometown != 'undefined') $(a).find('.friend-hometown').html(response.hometown.name);
				else $(a).find('.friend-hometown').html('');
				if(typeof response.location != 'undefined') $(a).find('.friend-current-location').html(response.location.name);
				else $(a).find('.friend-current-location').html('');
				if(typeof response.picture != 'undefined') $(a).find('.friend-picture').html('<img class="friend-profile-pic" src="'+response.picture +'" />');
				cnt++;
				response = null;
		});
	}
	document.getElementById('fwp-count').innerHTML = current_friend_index + ' / ' + num_friends;
	set_page_links();
}

function page_next() {
	if(friends != null && current_friend_index < num_friends) {
		current_friend_index++;
		document.getElementById('fwp-count').innerHTML = current_friend_index + ' / ' + num_friends;
		$('#friends-widget').children('.friends-widget-info').animate(
				{left : '-=400px'}
				);
	}
	set_page_links();
}

function page_prev() {
	if(friends != null && current_friend_index != 1) {
		current_friend_index--;
		document.getElementById('fwp-count').innerHTML = current_friend_index + ' / ' + num_friends;
		$('#friends-widget').children('.friends-widget-info').animate(
				{left : '+=400px'}
				);
	}
	set_page_links();
}

function set_page_links() {
	if(current_friend_index == 1) {
		$('#fwp-prev').attr('disabled', true);
	} else if(current_friend_index > 1) {
		$('#fwp-prev').attr('disabled', false);
	}
	if(current_friend_index == num_friends) {
		$('#fwp-next').attr('disabled', true);
	} else {
		$('#fwp-next').attr('disabled', false);
	}
}
